<?php

include( "./admin/include/func.php");

$db = new db();
if ( ! $db->connect()) exit( mysql_error());

$r_id = isset( $_REQUEST['id']) ? (int) $_REQUEST['id'] : 0; 

$db->sql = "SELECT id,nev,email,targy,szoveg,ar,telefon,weblap,kep,telepules,feladas,lejarat,
			(SELECT r.nev  FROM rovat r WHERE r.id=rovat) as rovat,
			(SELECT r.slug FROM rovat r WHERE r.id=rovat) as rovat_slug,
			(SELECT r.nev  FROM regio r WHERE r.id=regio) as regio  
			FROM hirdetes 
			WHERE id='$r_id' AND aktiv=1";				
$db->query();

$hird = mysql_fetch_assoc ($db->rs);

$hird_adat = 'Hirdetés tárgya: '.$hird['targy'].'
Hirdetés szövege:
'.$hird['szoveg'].'
Feladó: '.$hird['nev'].'
Email: '.$hird['email'].'
Telefon: '.$hird[ 'telefon'].'
Weblap: '.$hird['weblap'].'
Rovat: '.$hird ['rovat'].'
Régió: '.$hird['regio'].'
Feladva: '.$hird[ 'feladas'].'
Lejárat: '.$hird['lejarat'];

if (isset( $_POST['kuldes'])) {
	
	$p_felado_nev    = $_POST['felado_nev'];
	$p_felado_email  = $_POST['felado_email'];
	$p_cimzett_nev   = $_POST['cimzett_nev'];
	$p_cimzett_email = $_POST['cimzett_email'];

    $success = true;
    $errors = array();
    	
    if (($p_felado_email) != '' && !preg_match('/^[\.\+_a-z0-9-]+@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2}[mtgvu]?$/i', $p_felado_email)) {
        $success = false;
        array_push($errors, "Hibás Feladó email!");
    }
    
    if ($p_felado_email == '') {
        $success = false;
        array_push($errors, "A Feladó email nincs megadva!");
    }

    if (($p_cimzett_email) != '' && !preg_match('/^[\.\+_a-z0-9-]+@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2}[mtgvu]?$/i', $p_cimzett_email)) {
        $success = false;
        array_push($errors, "Hibás Címzett email!");
    }
    
    if ($p_cimzett_email == '') {
        $success = false;
        array_push($errors, "A Címzett email nincs megadva!");
    }

    if ($success) {
		
		$cimzett = "$p_cimzett_nev ($p_cimzett_email)";
		if ($p_cimzett_nev == '') $cimzett = $p_cimzett_email;
		
		$felado = "$p_felado_nev ($p_felado_email)";
		if ($p_felado_nev == '') $felado = $p_felado_email;
		
		$message = "Kedves $cimzett! 
   
Ezúton ajánlom figyelmedbe a " . $url . " oldalon talált alábbi hirdetést:

$url/hirdetesek.php?sorszam=".$hird['id']."

".$hird_adat."


Üdvözlettel:
$felado"; 

		mail( $p_cimzett_email, "Továbbított hirdetés", $message, "From: ".$noreply);  
     
		//print $message;
			
		/*
		 $dbKapcsolat = new DbKapcsolodas ();
		 if (! $dbKapcsolat->connect ()) exit ( "Unable to create a MySql connection <br>"); 

		 $sql = "INSERT INTO `emailLog` ( `eml_id` , `eml_hirdId` , `eml_from` , `eml_to` ) VALUES ( '', $hirdId, '".$feladoEmail."', '".$cimzettEmail."')";

		 //debug  ( "debug", $sql);   

		 $dbKapcsolat->setQueryString ( $sql);  
		 $dbKapcsolat->executeQuery ();
	   */	 
	}
}

?>

<?php include ("header.php"); ?>

<?php include ("top.php"); ?>

<?php include ("left.php"); ?>
							
<div id="middle">

<?php if (count($hird) < 2) { ?>

	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<p class="hpcaption">Hiba</p>
				<p class="hptitle">Hiba!</p>
			</div>	
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="25px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:25px">			
				<p class="success">A hirdetés nem található!</p>			
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="25px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>
		
<?php }	else { ?>
				
<?php if (isset($success) && $success) { ?>

	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<p class="hpcaption">Információ</p>
				<p class="hptitle">A hirdetést továbbítottuk!</p>
			</div>	
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="25px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:25px">			
				<p class="success">
					A hirdetést továbbítottuk a címzettnek!
				</p>			
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="25px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>

	<br />
	
	<br />

<?php } ?>

<?php if (isset($success) && ! $success) { ?>

	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<p class="hpcaption">Hiba</p>
				<p class="hptitle">Kérjük javítsa az alábbi hibákat!</p>
			</div>	
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="<?php print count($errors)*25+10 ?>px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:<?php print count($errors)*25+10 ?>px">			
				<ul class='errors'>
					<?php foreach ($errors as $err) print "<li>$err</li>"; ?>
				</ul>			
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="<?php print count($errors)*25+10 ?>px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>

	<br />
	
	<br />

<?php } ?>

<?php if (! isset($success) || (isset($success) && ! $success)) { ?>

	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<p class="hpcaption">Küldés emailben</p>
				<p class="hptitle">Hirdetés elküldése emailben</p>
			</div>	
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="510px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:510px">
				<form name="form_hirdkuldes" id="form_hirdkuldes" method="post" enctype='application/x-www-form-urlencoded' accept-charset="utf-8">

					<input name="id" type="hidden" value="<?php print $r_id; ?>" />
					
					<label for='felado' class="required">Feladó</label>
					<br />
					
					<label for='felado_nev'>Név</label>
					<input name='felado_nev' type='text' value="<?php if (isset( $p_felado_nev)) print $p_felado_nev; ?>" />
					<br />
					
					<label for='felado_email' class="required">Email</label>
					<input name='felado_email' type='text' value="<?php if (isset( $p_felado_email)) print $p_felado_email; ?>" />
					<br />
					
					<hr />
					<br />
										
					<label for='cimzett' class="required">Címzett</label>
					<br />
					
					<label for='cimzett_nev'>Név</label>
					<input name='cimzett_nev' type='text' value="<?php if (isset( $p_cimzett_nev)) print $p_cimzett_nev; ?>" />
					<br />
					
					<label for='cimzett_email' class="required">Email</label>
					<input name='cimzett_email' type='text' value="<?php if (isset( $p_cimzett_email)) print $p_cimzett_email; ?>" />
					<br />

					<hr />
					<br />
					
					<label for='hirdadat' class="required">Hirdetés adatai</label>
					<textarea cols="39" rows="14" id='hirdadat' name='hirdadat' cols='39' rows='14' readonly='readonly'><?php print $hird_adat; ?></textarea>
					<br />

					<br />
										
					<label for="kuldes">&nbsp;</label>
					<input name="kuldes" type="submit" value="Küldés"/>				

				</form>
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="510px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>

	<br />

<?php } ?>

<?php } ?>

<br />

</div>

<?php include ("right.php"); ?>

<?php include ("footer.php"); ?>
